package com.unionwo.service;

import java.sql.Timestamp;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.unionwo.commons.exception.DaoException;
import com.unionwo.commons.util.DateUtil;
import com.unionwo.dao.ComplainLogDao;
import com.unionwo.dto.ComplainInfoReq;
import com.unionwo.model.ComplainLog;
import com.unionwo.model.UnionResponse;

@Component("complainLogService")
public class ComplainLogService implements UnionService{
	
	private Logger log = Logger.getLogger(ComplainLogService.class);
	
	@Autowired
	private ComplainLogDao complainLogDao;
	Map<String,Long> countMap = new ConcurrentHashMap<String, Long>();
	
	public void insertComplainLog(HttpServletRequest request) throws DaoException{
		String userToken = request.getParameter("u");
		String telephoneNumber = request.getParameter("n");
		String semaphore = request.getParameter("s");
		String longitude = request.getParameter("lg");
		String latitude = request.getParameter("lt");
		String province = request.getParameter("p");
		String city = request.getParameter("c");
		String area = request.getParameter("a");
		String address = request.getParameter("r");
		String semaphoreFlag = String.valueOf(request.getAttribute("semaphoreFlag"));
		ComplainInfoReq c = new ComplainInfoReq(userToken, telephoneNumber,Double.parseDouble(semaphore),  semaphoreFlag,  Double.parseDouble(longitude),
				Double.parseDouble(latitude), province,  city,  area,address,DateUtil.getCurrentDateTimeStr()) ;
		//decodeComplianLog(c);
		ComplainLog cLog = null;
		cLog = revert2ComplainLogFromDto(c);
		String key = longitude+"*"+latitude;
		Long l = countMap.get(key);
		if(null==l){
			countMap.put(key, 1l);
		}else{
			countMap.put(key, ++l);
		}
		
		try {
			complainLogDao.insertComplainLog(cLog);
		} catch (Exception e1) {
			log.error("record "+userToken+" insert failure."+e1);
		}
		
	}
	
	
	public ComplainLog revert2ComplainLogFromDto(ComplainInfoReq ComplainInfoReq){
		if(null==ComplainInfoReq){
			throw new IllegalArgumentException("agruments null.");
		}
		ComplainLog complainLog = new ComplainLog();
		complainLog.setAddress(ComplainInfoReq.getAddress());
		complainLog.setArea(ComplainInfoReq.getArea());
		complainLog.setCity(ComplainInfoReq.getCity());
		complainLog.setInfo("");
		complainLog.setLatitude(ComplainInfoReq.getLatitude());
		complainLog.setLongitude(ComplainInfoReq.getLongitude());
		complainLog.setLongLat(ComplainInfoReq.getLatitude()+"*"+ComplainInfoReq.getLongitude());
		complainLog.setNetType("");
		complainLog.setPhoneType("");
		complainLog.setProblemInfo(0);
		complainLog.setProblemScenario(0);
		complainLog.setProvince(ComplainInfoReq.getProvince());
		complainLog.setSemaphore(ComplainInfoReq.getSemaphore());
		complainLog.setTelephoneNumber(ComplainInfoReq.getTelephoneNumber());
		complainLog.setTeye("");
		complainLog.setTimes(Timestamp.valueOf(ComplainInfoReq.getTime())); 
		complainLog.setCreateTime(new Timestamp(System.currentTimeMillis()));
		return complainLog;
	}


	@Override
	public UnionResponse doRequest(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
